/*
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
 *
 * Copyright (c) 2013-2014 jfinal app. jfapp Group.
 */

define(['adminapp', 'core/datatable', 'bootstrap-datepicker', 'bootstrap-datepicker-zh'], function ($app) {
    $app.navactive('game_stat');

    /*$('.date-picker').datepicker({
        language      : 'zh-CN',
        rtl: App.isRTL(),
        autoclose: true,
        format : 'yyyy-mm-dd'
    });*/

    $('button[name="stat_btn"]').click(function() {
        var gameId = $(this).attr("data-game");
        var $table = $('#game_stat_' + gameId);
        var start = $table.find('input[name="start"]').val();
        var end = $table.find('input[name="end"]').val();

        $.ajax({
            type: "POST",
            cache: false,
            data: {gameId:gameId,start:start,end:end},
            url: g.ctx + 'admin/stat/game/joinStat',
            dataType: "json",
            beforeSend: function() {
                App.blockUI({target: $table, iconOnly: true});
            },
            success: function(rets) {
                if (rets.status == 'OK') {
                    $.each(rets.data, function(k,v) {
                        $table.find('td[name="'+ k +'"]').html(v)
                    });
                } else {
                    $table.find('tbody').html(rets.message);
                }
            },
            complete: function() {
                App.unblockUI($table);
            }
        });
    });

    var winning_tpl = Handlebars.compile($(g.winningTpl).html());
    var rece_tpl = Handlebars.compile($(g.receTpl).html());

    var $listtable = $app.listtable({
        'sAjaxSource' : g.ctx + "admin/stat/game/joinStatList",
        'fnServerParams': function( aoData){
            var sd = $('#search_form').serializeJSON();
            $.each(sd, function(name, val){
                aoData.push({"name": name, "value": val});
            });
        },
        'aoColumns'   : [
            $app.checkall,
            $app.autono,
            {'mDataProp': 'join_time' },
            {'mDataProp': 'gname', bSortable: false },
            {'mDataProp': 'mname', bSortable: false },
            {'mDataProp': 'card_no', bSortable: false },
            {'mDataProp': 'phone', bSortable: false },
            {'mDataProp': 'winning', bSortable: false },
            {'mDataProp': 'gift_name', bSortable: false },
            {'mDataProp': 'receive', bSortable: false },
            {'mDataProp': 'receive_time', bSortable: false }
        ],
        'aoColumnDefs': [
            {
                'aTargets': [7],
                'mData'   : null,
                'mRender' : function (data, type, full) {
                    return winning_tpl(full);
                }
            },
            {
                'aTargets': [9],
                'mData'   : null,
                'mRender' : function (data, type, full) {
                    return rece_tpl(full);
                }
            }
        ]
    }, 'data-list');

    $('#search_btn').on('click', function (e) {
        e.preventDefault();
        $listtable.fnReloadAjax();
    });

    $('a[name="winingPeople"]').click(function() {
        var gameId = $(this).attr("data-game");
        var $table = $('#game_stat_' + gameId);
        var start = $table.find('input[name="start"]').val();
        var end = $table.find('input[name="end"]').val();

        $("#start").val(start);
        $("#end").val(end);

        $("#game").select2("val", gameId);
        $('#winning').select2("val", "true");
        $listtable.fnReloadAjax();
        //$('#game')
        //$('#winning')
    });
});

